package com.ldj.struts.action;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.MenuBuilder;
import com.ldj.common.SystemConfig;
import com.ldj.common.SystemSession;
import com.ldj.common.Upload;
import com.ldj.hibernate.FunctionMenuDAO;
import com.ldj.hibernate.PersionFile;
import com.ldj.hibernate.PersionFileDAO;
import com.ldj.hibernate.SuperviseOrgan;
import com.ldj.hibernate.SuperviseOrganDAO;
import com.ldj.hibernate.SysLogInfo;
import com.ldj.hibernate.SysLogInfoDAO;
import com.ldj.hibernate.UserAreaScope;
import com.ldj.hibernate.UserManagerScope;
import com.ldj.struts.form.PersionFileForm;
public class PersionFileAction extends DispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		PersionFileForm pForm = (PersionFileForm)form;
		PersionFileDAO dao = new PersionFileDAO(request, response);
		String code = request.getParameter("code");
		if(code !=null && !"".equals(code)){		
			request.getSession().setAttribute("code", code);
		}
		code = (String)request.getSession().getAttribute("code");
		List list = dao.findAllPage(pForm,code);
		request.setAttribute("list", list);
		request.setAttribute("intPage", String.valueOf(dao.intPage));
		return mapping.findForward("list");
	}
	
	public ActionForward detail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		PersionFileDAO dao = new PersionFileDAO(request, response);
		SuperviseOrganDAO sDAO = new SuperviseOrganDAO(request, response);
		PersionFile info = dao.findById(request.getParameter("pid"));
		String code = info.getPerFileOrganCode();
		SuperviseOrgan sup=(SuperviseOrgan)sDAO.findBySupOrganCode(code).get(0);
		request.setAttribute("eInfo", info);
		request.setAttribute("name", sup.getSupOrganName());
		return mapping.findForward("detail");
	}
	
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("toAdd");
	}
	
	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		PersionFileDAO dao = new PersionFileDAO(request, response);
		PersionFile info = dao.findById(request.getParameter("pid"));
		request.setAttribute("pInfo", info);
		request.setAttribute("nowPage",request.getParameter("page"));
		return mapping.findForward("toUpdate");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		PersionFileForm pForm = (PersionFileForm)form;
		PersionFileDAO dao = new PersionFileDAO(request, response);
		PersionFile info = dao.findById(request.getParameter("pid"));
		
		info.setPerFileEnfoceCardNum(pForm.getPerFileEnfoceCardNum());
		info.setPerFileSupCardNum(pForm.getPerFileSupCardNum());
		info.setPerFileIdCardNum(pForm.getPerFileIdCardNum());
		info.setPerFileName(pForm.getPerFileName());
		info.setPerFileSex(pForm.getPerFileSex());
		info.setPerFileDuty(pForm.getPerFileDuty());
		info.setPerFileBirthday(pForm.getDatePerFileBirthday());
		info.setPerFileNativePlace(pForm.getPerFileNativePlace());
//		info.setPerFileLoginName(pForm.getPerFileLoginName());
		info.setPerFileLoginPass(pForm.getPerFileLoginPass());
		info.setPerFilePhotoUrl(pForm.getPerFilePhotoUrl());
		info.setPerFileEducationLevel(pForm.getPerFileEducationLevel());
		info.setPerFilePoliticalStatus(pForm.getPerFilePoliticalStatus());
		dao.merge(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("1");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "PersionFile.update").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getPerFileName()));
		logDao.save(s);
		return new ActionForward("/PersionFile.do?method=list&pages="+request.getParameter("pages"), true);
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		PersionFileForm pForm = (PersionFileForm)form;
		PersionFileDAO dao = new PersionFileDAO(request, response);
		List list = dao.findByLoginName(pForm.getPerFileLoginName());
		if(list != null && list.size() > 0){
			request.setAttribute("error", "error");
		}else{
			PersionFile info=new PersionFile();
			String code = (String)request.getSession().getAttribute("code");
			info.setPerFileOrganCode(code);
			info.setPerFileEnfoceCardNum(pForm.getPerFileEnfoceCardNum());
			info.setPerFileSupCardNum(pForm.getPerFileSupCardNum());
			info.setPerFileIdCardNum(pForm.getPerFileIdCardNum());
			info.setPerFileName(pForm.getPerFileName());
			info.setPerFileSex(pForm.getPerFileSex());
			info.setPerFileDuty(pForm.getPerFileDuty());
			info.setPerFileBirthday(pForm.getDatePerFileBirthday());
			info.setPerFileNativePlace(pForm.getPerFileNativePlace());
			info.setPerFileLoginName(pForm.getPerFileLoginName());
			info.setPerFileLoginPass(pForm.getPerFileLoginPass());
			info.setPerFilePhotoUrl(pForm.getPerFilePhotoUrl());
			info.setPerFileEducationLevel(pForm.getPerFileEducationLevel());
			info.setPerFilePoliticalStatus(pForm.getPerFilePoliticalStatus());
			//info.setPerFileCheck(pForm.getPerFileCheck());
			info.setPerFileDelOper("0");
			/*List<StaffOrgan> staffList= sDAO.findByPersionFileId(info.getPerFileId());
			for(int i=0;i<staffList.size();i++){
			}*/
			dao.save(info);
			
			SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
			SysLogInfo s = new SysLogInfo();
			s.setPersionFile(SystemSession.getSupUserInfo(request));
			s.setSysLogInfoOperDate(new Date());
			s.setSysLogInfoOperType("0");
			s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
			s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "PersionFile.add").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getPerFileName()));
			logDao.save(s);
		}
		return mapping.findForward("isHave");
//		return new ActionForward("/PersionFile.do?method=list", true);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		PersionFileDAO dao = new PersionFileDAO(request, response);
		PersionFile info = dao.findById(request.getParameter("pid"));
		info.setPerFileDelOper("1");
		dao.merge(info);
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("2");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "PersionFile.delete").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getPerFileName()));
		logDao.save(s);
		return new ActionForward("/PersionFile.do?method=list&pages="+request.getParameter("page"), true);
	}
	
	public ActionForward upload(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			HashMap map = Upload.upload(request, "photo", ".jpg;.jpeg;.gif");
			if(map.containsKey("fileName")){
				request.setAttribute("fileName", (String)map.get("fileName"));
			}else{
				request.setAttribute("error", "error");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return mapping.findForward("upload");
	}
	
	public ActionForward tree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String value = "";
		PersionFileDAO dao = new PersionFileDAO(request,response);
		List<UserManagerScope> infoList = dao.findBySuperviseOrganId(SystemSession.getSupUserId(request));
		if(infoList != null && infoList.size()>0){
			UserManagerScope info1 =infoList.get(0);
			value = info1.getUserManagerScopeOrganId();
		}
		MenuBuilder bu = new MenuBuilder();
		request.setAttribute("tree", bu.getPowerSuperviseOrganTree(request, response, "监察机构", "", "PersionFile.do?method=list&code=$code", value));		
		return mapping.findForward("tree");
	}	
	
	public ActionForward slectInspectorTree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		MenuBuilder bu = new MenuBuilder();
		request.setAttribute("tree", bu.getSuperviseOrganTree(request, response, "监察机构", "", "PersionFile.do?method=selectInspectorList&code=$code" , null));		
		return mapping.findForward("tree");
	}
	public ActionForward selectInspectorList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		PersionFileForm pForm = (PersionFileForm)form;
		PersionFileDAO dao = new PersionFileDAO(request, response);
		String code = request.getParameter("code");
		if(code !=null && !"".equals(code)){		
			request.getSession().setAttribute("code", code);
		}
		code = (String)request.getSession().getAttribute("code");
		List list = dao.findAllPage(pForm, code);
		request.setAttribute("list", list);
		return mapping.findForward("selectInspectorList");
	}
	
	
	public ActionForward organUsertree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		MenuBuilder bu = new MenuBuilder();
		request.setAttribute("tree", bu.getSuperviseOrganTree(request, response, "监察机构", "PersionFile.do?method=selectUserList&code=9999", "PersionFile.do?method=selectUserList&code=$code", null));		
		return mapping.findForward("tree");
	}
	public ActionForward selectUserList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		PersionFileForm pForm = (PersionFileForm)form;
		PersionFileDAO dao = new PersionFileDAO(request, response);
		String code = request.getParameter("code");
		if(code !=null && !"".equals(code)){		
			request.getSession().setAttribute("code", code);
		}
		code = (String)request.getSession().getAttribute("code");
		List list = dao.findAll(pForm,code);
		request.setAttribute("list", list);
		return mapping.findForward("select");
	}
	
	public ActionForward selectUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("selectUser");
	}
	
	
	public ActionForward selectInspector(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		PersionFileForm pForm = (PersionFileForm)form;
		PersionFileDAO dao = new PersionFileDAO(request, response);
		String code = request.getParameter("code");
		if(code !=null && !"".equals(code)){		
			request.getSession().setAttribute("code", code);
		}
		code = (String)request.getSession().getAttribute("code");
		List list = dao.findAll(pForm,code);
		request.setAttribute("list", list);
		return mapping.findForward("selectInspector");
	}
	
	public ActionForward toSelectInspector(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("toSelectInspector");
	}
	
	
	public ActionForward superviseOrganTree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
	//	String code = request.getParameter("code");
		SuperviseOrganDAO dao = new SuperviseOrganDAO(request,response);
		PersionFileDAO pDAO = new PersionFileDAO(request,response);
		String pid = request.getParameter("pid");
		PersionFile pInfo=pDAO.findById(pid);
		String username = pInfo.getPerFileName();
		List<UserManagerScope> infoList = dao.findBySupCode(pid);
		String value = "";
		if(infoList != null && infoList.size()>0){
			UserManagerScope info = infoList.get(0);
			value = info.getUserManagerScopeOrganId();
		}
		if(value == null)
			value = "";
		MenuBuilder bu = new MenuBuilder();
		request.setAttribute("username", username);
		request.setAttribute("tree", bu.getSuperviseOrganTree(request, response, "监察机构", "", "", value));		
		return mapping.findForward("superviseOrganTree");
	}
	
	public ActionForward functionMenuTree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		FunctionMenuDAO dao = new FunctionMenuDAO(request,response);
		PersionFileDAO pDAO = new PersionFileDAO(request,response);
		String pid = request.getParameter("pid");
		PersionFile pInfo=pDAO.findById(pid);
		String username = pInfo.getPerFileName();
		String value = "";
		List<UserAreaScope> infoList = dao.findByUserId(pid);
		if(infoList != null && infoList.size()>0){
			UserAreaScope info = infoList.get(0);
			value = info.getUserAreaScopeAreaId();
		}
		if(value == null)
			value = "";
		MenuBuilder bu = new MenuBuilder();
		request.setAttribute("username", username);
		request.setAttribute("tree", bu.getAreaTree(request, response, "地区", "", "", value));		
		return mapping.findForward("functionMenuTree");
	}
	
	public ActionForward addSuper(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		SuperviseOrganDAO dao = new SuperviseOrganDAO(request,response);
		PersionFileDAO pDAO = new PersionFileDAO(request,response);
		String pid = request.getParameter("pid");
		PersionFile pInfo=pDAO.findById(pid);
		String username = pInfo.getPerFileName();
		String[] code= request.getParameterValues("functionMenuCode");
		String codeString="";
		if(code != null && code.length > 0)
			for(int i=0;i<code.length;i++){
				 codeString+=code[i] + ",";
			}
		if(codeString.length() > 0)
			codeString = codeString.substring(0, codeString.length() - 1);
		List<UserManagerScope> infoList = dao.findBySupCode(pid);
		UserManagerScope info = new UserManagerScope();
		if(infoList != null && infoList.size()>0){
			UserManagerScope info1 = infoList.get(0);
			info.setUserManagerScopeOrganId(codeString);
			info.setUserManagerScopeUserId(pid);
			dao.deleteSuper(info1);
		}else{
			info.setUserManagerScopeOrganId(codeString);
			info.setUserManagerScopeUserId(pid);
		}
		dao.saveSuper(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("6");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "UserManagerScope").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", username));
		logDao.save(s);
		return new ActionForward("/RoleInfo.do?method=showUser", true);
		
	}	
	
	public ActionForward addArea(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		FunctionMenuDAO dao = new FunctionMenuDAO(request,response);
		PersionFileDAO pDAO = new PersionFileDAO(request,response);
		String pid = request.getParameter("pid");
		PersionFile pInfo=pDAO.findById(pid);
		String username = pInfo.getPerFileName();
		String[] code= request.getParameterValues("functionMenuCode");
		String codeString="";
		if(code != null && code.length > 0)
			for(int i=0;i<code.length;i++){
				 codeString+=code[i] + ",";
			}
		if(codeString.length() > 0)
		codeString = codeString.substring(0, codeString.length() - 1);
		List<UserAreaScope> infoList = dao.findByUserId(pid);
		UserAreaScope info = new UserAreaScope();
		if(infoList != null && infoList.size()>0){
			UserAreaScope info1 = infoList.get(0);
			info.setUserAreaScopeUserId(pid);
			info.setUserAreaScopeAreaId(codeString);
			dao.deleteArea(info1);
		}else{
			info.setUserAreaScopeUserId(pid);
			info.setUserAreaScopeAreaId(codeString);
		}
		dao.saveArea(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("7");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "UserAreaScope").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", username));
		logDao.save(s);
		return new ActionForward("/RoleInfo.do?method=showUser", true);
		
	}
	
	public ActionForward toCheckScore(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		PersionFileDAO dao = new PersionFileDAO(request, response);
		PersionFile info = dao.findById(request.getParameter("pid"));
		request.setAttribute("pInfo", info);
		return mapping.findForward("toCheckScore");
	}
	
	public ActionForward checkScore(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		PersionFileForm pForm = (PersionFileForm)form;
		PersionFileDAO dao = new PersionFileDAO(request, response);
		PersionFile info = dao.findById(request.getParameter("pid"));
		PersionFile pInfo =(PersionFile) request.getSession().getAttribute("UserInfo");

		info.setPerFileCheck(pForm.getPerFileCheck());
		info.setPerFileCheckDate(new Date());
		info.setPerFileCheckUserId(pInfo.getPerFileId());
		dao.merge(info);
		return new ActionForward("/PersionFile.do?method=list", true);
	}
	
}